package lunchy.data.classes;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import lunchy.data.interfaces.ILunchyDataConnector;
import lunchy.data.interfaces.ILunchyMenuDAO;
import lunchy.data.interfaces.ILunchyOrderDAO;
import lunchy.data.interfaces.ILunchyUsersDAO;

public class CLunchyXMLConnector implements ILunchyDataConnector
{
	private ILunchyMenuDAO XMLMenuData;
	private ILunchyOrderDAO XMLOrderData;
	private ILunchyUsersDAO XMLUsersData;
	
	public void CLunchyXMLConnector()
	{
		
	}
	
	private Document loadXML(String file)
	{
		Document data = null;
		
		DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
		f.setValidating(false);
		
		DocumentBuilder builder = null;
		
		try
		{
			builder = f.newDocumentBuilder();
		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}

		try
		{
			data = builder.parse(new File("data/" + file));
		}
		catch (SAXException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return data;
	}
	
	public ILunchyMenuDAO getMenuDAO()
	{
		if (XMLMenuData == null)
			XMLMenuData = new CLunchyXMLMenuDAO(loadXML("menu.xml"));
		
		return XMLMenuData; 
	}
	
	public ILunchyOrderDAO getOrderDAO()
	{
		if (XMLOrderData == null)
			XMLOrderData = new CLunchyXMLOrderDAO(loadXML("orders.xml"));
		
		return XMLOrderData;
	}
	
	public ILunchyUsersDAO getUsersDAO()
	{
		if (XMLUsersData == null)
			XMLUsersData = new CLunchyXMLUsersDAO(loadXML("users.xml"));
		
		return XMLUsersData;
	}
}
